package cards.controller;

import cards.dao.CardActivityDao;
import cards.dao.UserDao;
import cards.entity.CardActivity;
import cards.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cards.service.CardActivityService;
import cards.util.HostHolder;

import javax.servlet.http.HttpServletRequest;


@Controller
public class DataController {

        @Value("${path.upload}")
        private String uploadPath;
        @Value("${path.domain}")
        private String domain;
        private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
        @Autowired
        private CardActivityService cardActivityService;
        @Autowired
        private UserDao userDao;
        //    private ImageDao imageDao;
        @Autowired
        private CardActivityDao cardActivityDao;

        //
        @Autowired
        private HostHolder hostHolder;

        @RequestMapping(path = "/data/{id}", method = RequestMethod.GET)
        public String displaycardActivitiesByUser(@PathVariable String id, Model model) {
            User user = hostHolder.getUser();
            CardActivity cardActivity = cardActivityService.findCardActivityById(id);
            model.addAttribute("user", user);
            model.addAttribute("cardActivity", cardActivity);
            return "data";
        }

        @RequestMapping(path="/data/{id}/{type}",method=RequestMethod.POST)
        public String addCardActivity(@PathVariable String type, @PathVariable String id,
                HttpServletRequest request, Model model) {
            User user = hostHolder.getUser();
            CardActivity cardActivity = cardActivityService.findCardActivityById(id);
            model.addAttribute("user", user);

            if (type.equals("on")) {cardActivityDao.updateStatus(cardActivity.getId(),1);}
            else {cardActivityDao.updateStatus(cardActivity.getId(),0);}
            return "redirect:/admin/statics";
        }

}


